package com.eastfair.imaster.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eastfair.boot.controller.SuperController;
import com.eastfair.boot.request.PageParams;
import com.eastfair.core.base.R;
import com.eastfair.echo.core.EchoService;
import com.eastfair.imaster.dto.NewsDTO;
import com.eastfair.imaster.dto.NewsPageQuery;
import com.eastfair.imaster.dto.NewsSaveDTO;
import com.eastfair.imaster.dto.NewsUpdateDTO;
import com.eastfair.imaster.entity.News;
import com.eastfair.imaster.service.NewsService;
import com.eastfair.imaster.vo.NewsVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 新闻前端控制器
 *
 * </p>
 *
 * @author zjn
 * @date 2021-08-31
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/news")
@Api(value = "News", tags = "")
public class NewsController extends SuperController<NewsService, Long, News, NewsPageQuery, NewsSaveDTO, NewsUpdateDTO> {

    @Autowired
    private EchoService echoService;

    @Autowired
    private NewsService newsService;

    @Override
    public void handlerResult(IPage<News> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }

    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list) {
        List<News> newsList = list.stream().map((map) -> {
            News news = News.builder().build();
            //TODO 请在这里完成转换
            return news;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(newsList));
    }

    /**
     * 删除新闻
     *
     * @param id 新闻
     * @return 是否删除成功
     */
    @ApiOperation(value = "删除新闻", notes = "删除新闻")
    @DeleteMapping("/{id}")
    public R<Boolean> deleteById(@PathVariable("id") Long id) {
        return R.success(newsService.deleteById(id));
    }

    /**
     * 修改新闻
     *
     * @param newsDTO 新闻实体
     * @return 是否修改成功
     */
    @ApiOperation(value = "修改新闻", notes = "修改新闻")
    @PutMapping("/updateById")
    public R<Boolean> updateById(@RequestBody @Validated({NewsDTO.VerifyUpdate.class}) NewsDTO newsDTO) {
        return R.success(newsService.updateById(newsDTO));
    }

    /**
     * 通过id获取新闻实体
     *
     * @param id 主键
     * @return 新闻实体
     */
    @ApiOperation(value = "获取新闻实体", notes = "获取新闻实体")
    @GetMapping("/queryById/{id}")
    public R<NewsVO> queryById(@PathVariable("id") Long id) {
        return R.success(newsService.queryById(id));
    }

    /**
     * 获取分页新闻列表
     *
     * @param params 分页条件
     * @return 分页新闻列表
     */
    @ApiOperation(value = "获取分页新闻列表", notes = "获取分页新闻列表")
    @PostMapping("/queryPageList")
    public R<Page<NewsVO>> queryPageList(@RequestBody PageParams<NewsPageQuery> params) {
        return R.success(newsService.queryPageList(params));
    }

    /**
     * 获取新闻列表
     *
     * @param newsDTO 新闻筛选条件
     * @return 新闻列表
     */
    @ApiOperation(value = "获取新闻列表", notes = "获取新闻列表")
    @PostMapping("/queryList")
    public R<List<NewsVO>> queryList(@RequestBody NewsDTO newsDTO) {
        return R.success(newsService.queryList(newsDTO));
    }

}
